﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace Mystery.SharePoint
{
	/// <summary>
	/// Extensions for SPFeaturePropertyCollection 
	/// </summary>
	public static class SPFeaturePropertyCollectionExtensions
	{
		/// <summary>
		/// Reads a value from the feature property collection
		/// </summary>
		/// <param name="collection"></param>
		/// <param name="name">The name of the property</param>
		/// <param name="defaultValue">A value to use if no property was found or set</param>
		/// <returns>A string representing the value of the property or the default value</returns>
		public static string Read(this SPFeaturePropertyCollection collection, string name, string defaultValue)
		{
			SPFeatureProperty p = collection[name];
			if ((p != null) &&
				(string.IsNullOrEmpty(p.Value) == false))
			{
				return p.Value;
			}
			return defaultValue;
		}
		/// <summary>
		/// Writes a value to the property collection
		/// </summary>
		/// <param name="collection"></param>
		/// <param name="name">Name of property</param>
		/// <param name="value">Value of property</param>
		/// <returns>True if the property bag was updated</returns>
		public static bool Write(this SPFeaturePropertyCollection collection, string name, string value)
		{
			SPFeatureProperty p = collection[name];
			if ((p != null) &&
				(p.Value.Equals(value, StringComparison.InvariantCultureIgnoreCase) == false))
			{
				p.Value = value;
				return true;
			}
			return false;
		}
	}
}
